package com.navigationtest;

import android.view.KeyEvent;

import com.myatest.BR;
import com.myatest.R;
import com.pbase.tools.Logger;
import com.pbase.ui.page.DataBindingConfig;

import androidx.activity.OnBackPressedCallback;

/**
 * User: psn
 * Date: 2021/12/6
 * Navigation 测试，
 * 2021 12 20 viewpage,与Navigation联用每个viewpage子页对应一个独立的导航图，与预想不符
 * viewpage子页均为独立的导航图，但跳转都放到activity中执行
 * https://juejin.cn/post/7036296113573347364 参考
 *
 * ==================================================================
 * 返回键测试结论
 * 1、不应在基activity中拦截放键
 * 2、fragment使用requireActivity().getOnBackPressedDispatcher()
 *                 .addCallback() 拦截返回键操作，并通过AppManager判断当前fragment做区分操作
 * 3.fragment的view中依然可以设置View.OnKeyListener监听键盘事件
 * 4、getOnBackPressedDispatcher().addCallback()后设置的顶替先设置的
 * 5、
 * ==================================================================
 */
public class NavigationMainActivity extends RouterActivity {
    private NavigationViewModel mViewModel;

    @Override
    protected void onCreateBefore() {
    getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            //拦截返回事件
//            moveTaskToBack(true);
            Logger.e(getClass(),"NavigationMainActivity handleOnBackPressed");
//            finish();
        }
    });
    }

    @Override
    public void onBackPressed() {
        Logger.e(getClass(),"NavigationMainActivity handleOnBackPressed 2");
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Logger.e(getClass(),"NavigationMainActivity handleOnBackPressed 1");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreateAfter() {

    }

    @Override
    protected void initViewModel() {
        mViewModel = getActivityScopeViewModel(NavigationViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.act_navigation, BR.vm,mViewModel);
    }
//    @Override
//    public boolean onSupportNavigateUp() {
//        return Navigation.findNavController(this, R.id.nav_graph).navigateUp();
//    }
}
